/*
 * Copyright (C) 2016-2017 AnStudio
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.astudio.lhasa.workers.synthesisers;

import android.content.Context;
import android.os.Bundle;

import com.astudio.lhasa.MyApplication;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

public class OnlineSynthesiser implements InitListener, SynthesizerListener {
    private static final String TAG = OnlineSynthesiser.class.getSimpleName();

    private String mRole = SynthesizerRole.SYNTHESIZER_ROLE_XIAOYAN;
    private int mSpeed = 50;
    private int mVolume = 80;
    private Context mContext = null;
    private static volatile OnlineSynthesiser mInstance = null;
    private SpeechSynthesizer mSynthesizer = null;
    private int mCurCode = -1;

    private OnlineSynthesiser() {
        mSynthesizer = SpeechSynthesizer.createSynthesizer(
                MyApplication.getTIApplication(), this);
        mSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
    }

    public static OnlineSynthesiser getSynthesizer() {
        return getSynthesizer(SynthesizerRole.SYNTHESIZER_ROLE_XIAOYAN, 50, 80);
    }

    public static OnlineSynthesiser getSynthesizer(String role, int speed, int vol) {
        if (mInstance == null) {
            synchronized (OnlineSynthesiser.class) {
                mInstance = new OnlineSynthesiser();
            }
        }

        mInstance.setRole(role);
        mInstance.setOutputSpeechSpeed(speed);
        mInstance.setOutputSpeechVolume(vol);

        return mInstance;
    }

    public void setRole(String role) {
        mRole = role;
        if (mSynthesizer != null){
            mSynthesizer.setParameter(SpeechConstant.VOICE_NAME, mRole);
        }
    }

    public void setOutputSpeechSpeed(int speed) {
        mSpeed = speed;
        if (mSynthesizer != null){
            mSynthesizer.setParameter(SpeechConstant.SPEED, "" + mSpeed);
        }
    }

    public int getOutputSpeechSpeed() {
        return mSpeed;
    }

    public void setOutputSpeechVolume(int vol) {
        mVolume = vol;
        if (mSynthesizer != null){
            mSynthesizer.setParameter(SpeechConstant.VOLUME, "" + mVolume);
        }
    }
    public int getOutputSpeechVolume() {
        return mVolume;
    }


    public void startSynthesize(int code, String input) {
        if (mSynthesizer != null) {
            mSynthesizer.startSpeaking(input, this);
        }

        mCurCode = code;
    }

    @Override
    public void onInit(int i) {

    }

    @Override
    public void onSpeakBegin() {

    }

    @Override
    public void onBufferProgress(int i, int i1, int i2, String s) {

    }

    @Override
    public void onSpeakPaused() {

    }

    @Override
    public void onSpeakResumed() {

    }

    @Override
    public void onSpeakProgress(int i, int i1, int i2) {

    }

    @Override
    public void onCompleted(SpeechError speechError) {

    }

    @Override
    public void onEvent(int i, int i1, int i2, Bundle bundle) {

    }
}
